package com.lemon.smartsafety.ui.adapter.common;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.entity.common.AreaVo;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.common.LoggerHelper;

import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/3/24.
 */
public class AreaAdapter extends LemonRcvAdapter<AreaVo> {
    private RecyclerTClickListener<AreaVo> mClickListener;
    private LoggerHelper loggerHelper = new LoggerHelper(AreaAdapter.class);

    private String mOldValue = null;

    private AreaAdapter(@Nullable List data) {
        super(data);
    }

    public AreaAdapter(@Nullable List data, RecyclerTClickListener<AreaVo> listener) {
        super(data);
        this.mClickListener = listener;
        this.mOldValue = "";
    }

    public AreaAdapter(@Nullable List data, RecyclerTClickListener<AreaVo> listener, String oldValue) {
        super(data);
        this.mClickListener = listener;
        this.mOldValue = oldValue;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object obj) {
        return new AreaItem(this.mOldValue, this.mClickListener);
    }


    public class AreaItem implements AdapterItem<AreaVo> {
        private RecyclerTClickListener<AreaVo> mClickListener;


        private AreaVo mData;
        private int    mPosition;
        private String mOldValue = null;


        private LinearLayout ripple;
        private TextView   itemText;
        private ImageView  imageArrow;


        public AreaItem(String oldValue, RecyclerTClickListener<AreaVo> clickListener) {
            this.mClickListener = clickListener;
            this.mOldValue = oldValue;
        }


        @Override
        public int getLayoutResId() {
            return R.layout.item_area;
        }

        @Override
        public void bindViews(View root) {
            ripple = (LinearLayout) root.findViewById(R.id.ripple);
            itemText = (TextView) root.findViewById(R.id.itemText);
            imageArrow = (ImageView) root.findViewById(R.id.imageArrow);
        }

        @Override
        public void setViews() {
            ripple.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mClickListener.onItemClick(mData, mPosition);
                }
            });
        }

        @Override
        public void handleData(AreaVo data, int position) {
            mData = data;
            mPosition = position;
            itemText.setText(data.getTitle());
            if (data.isHasChild()) {
                imageArrow.setVisibility(View.VISIBLE);
            }else{
                imageArrow.setVisibility(View.INVISIBLE);
            }
        }
    }
}
